package softstarter.gui.subareas.commandline;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import softstarter.gui.subareas.FireActionsController;

public class CommandLineContent extends PlainDocument {

	private boolean changinItemOrder;
	private CommandLineArea commandLine;
	private FireActionsController fireActionsController;
	
	public CommandLineContent(CommandLineArea commandLine, FireActionsController fireActionsController) {
		this.commandLine = commandLine;
		this.fireActionsController = fireActionsController;
		changinItemOrder = false;
	}
	
	@Override
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException {
		super.insertString(offs, str, a);
		
		handleUpdateToCommandLine();
	}

	@Override
	protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) {
		super.insertUpdate(chng, attr);
		
		handleUpdateToCommandLine();
	}

	@Override
	protected void removeUpdate(DefaultDocumentEvent chng) {
		super.removeUpdate(chng);
		
		handleUpdateToCommandLine();
	}
	
	private void handleUpdateToCommandLine() {
		
		if(!changinItemOrder) {
			changinItemOrder = true;
			
			try {
				String searchWord = getText(0, getLength());
				System.out.println(searchWord);
				fireActionsController.updateTopHitsListToCommandLine(searchWord);

			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		changinItemOrder = false;
		
	}

	
}
